
import { createStore } from 'vuex'
const defaultState = {
  count: 0,
  routerInfo: [{
    meta: { title: '概况' },
    name: "Home",
    path: "/home",
  }],//导航菜单
  getRouterList: [
    {
      path: '/',
      name: 'Home',
      meta: {
        title: '员工管理',
        icon: 'HomeFilled'
      },
      children: [
        {
          path: '/general',
          name: 'General',
          meta: {
            title: '概况',
            icon: 'Histogram'
          },
        }, {
          path: '/home',
          name: 'Home',
          meta: {
            title: '员工信息',
            icon: 'Avatar'
          },
        },
      ]
    },
    {
      path: '/',
      name: 'Recruit',
      meta: {
        title: '招聘管理',
        icon: 'Promotion'
      },
      children: [
        {
          path: '/recruit',
          name: 'Recruit',
          meta: {
            title: '招聘岗位',
            icon: 'MessageBox'
          },
        }, {
          path: '/apply',
          name: 'Apply',
          meta: {
            title: '应聘者信息',
            icon: 'FolderRemove'
          },
        },
      ]
    },
    {
      path: '/',
      name: 'Clocking',
      meta: {
        title: '考勤管理',
        icon: 'Calendar'
      },
      children: [
        {
          path: '/clocking',
          name: 'Clocking',
          meta: {
            title: '考勤信息',
            icon: 'Coin'
          },
        }, {
          path: '/sign',
          name: 'Sign',
          meta: {
            title: '签到信息',
            icon: 'Place'
          },
        },
      ]
    }
  ], //左侧菜单
  navList: [],//导航菜单缓存
  authList: ['Home', 'Recruit', 'Apply', 'General', 'Clocking', 'Sign']  //登录鉴权
}

const permission = (data) => {
  data.forEach(item => {
    if (item.children) {
      item.children = permission(item.children)
    }
  })
  return data
}
// Create a new store instance.
export default createStore({

  state() {
    return defaultState
  },
  mutations: {
    increment(state: typeof defaultState, routeList) {
      state.routerInfo = routeList
      localStorage.setItem("navList", JSON.stringify(state.routerInfo))
    },
    getRouterInfo(state: typeof defaultState, router) {
      state.routerInfo.push(router)
      let newobj = {};
      state.routerInfo = state.routerInfo.reduce((preVal, curVal) => {
        newobj[curVal.name] ? '' : newobj[curVal.name] = preVal.push(curVal);
        return preVal
      }, [])
      localStorage.setItem("navList", JSON.stringify(state.routerInfo))
    },
    getRouter(state) {
      let data = permission(state.getRouterList)
      state.navList = data
    },
      SET_AUTH_LIST(state, authList) {
      state.authList = authList
    }
  },
  actions: {
    getrouterList(context) {
      context.commit("getRouter")
    },
    increment(context, routeList) {
      context.commit('increment', routeList)
    },
    getRouterInfo(context, router) {
      context.commit('getRouterInfo', router)
    }
  },
  getters: {
  }
})